package control;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.*;
public class AddMessage extends HttpServlet {
    public AddMessage() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String n = request.getParameter("n");
		String t = request.getParameter("t");
		String m = request.getParameter("m");
		String uid = request.getParameter("uid");
		Message mg = new Message(0, Integer.parseInt(uid), n,t, m);
		SetUp setUp = new SetUp();
		String checkt = mg.checkMGT();
		String checkn = mg.checkMGName("");
		String checkm = mg.checkMG();
		if (!(checkn.equals("")&&(checkm.equals(""))&&(checkt.equals("")))) {
			request.setAttribute("n", mg.getM_name());
			request.setAttribute("m",mg.getM_content());
			request.setAttribute("t",mg.getM_title());
			request.setAttribute("en", checkn);
			request.setAttribute("et", checkt);
			request.setAttribute("em",checkm);
			RequestDispatcher transferexist = getServletContext().getRequestDispatcher("/a/messages.jsp?site=a");
			transferexist.forward(request, response);
		} else {
			mg.Create();
			request.setAttribute("m", "add-mgok");
			request.setAttribute("r","vm");
			RequestDispatcher transfer = getServletContext().getRequestDispatcher("/a/message.jsp");
			transfer.forward(request, response);
		}
	}

}
